package com.ziq.zcamp.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.ziq.base.extension.dp
import com.ziq.zcamp.R

class CampLifeBadgeView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) :FrameLayout(context, attrs, defStyleAttr) {
    val text : TextView
    val root : FrameLayout
    val image : ImageView

    init {
        val v = View.inflate(context, R.layout.view_camp_life_badge, this)
        text = v.findViewById(R.id.tv_camp_life_badge_content)
        root = v.findViewById(R.id.root)
        image = v.findViewById(R.id.iv_camp_life_badge_iamge)
    }

    @SuppressLint("SetTextI18n")
    fun setCampNum(num : Int?){
        if (num == null || num <= 0){
            text.visibility = View.GONE
            image.setImageResource(R.drawable.icon_camp_life_badge_empty)
            root.setBackgroundColor(Color.parseColor("#00000000"))
            val params = image.layoutParams as LayoutParams
            params.bottomMargin = 0
            params.topMargin = 0
            params.leftMargin = 0
            params.rightMargin = 0
            image.layoutParams = params
        }else{
            text.visibility = View.VISIBLE
            image.setImageResource(R.drawable.icon_camp_life_badge)
            root.setBackgroundResource(R.drawable.bg_camp_life_badge)
            text.text = "露营${num}次"
            val params = image.layoutParams as LayoutParams
            params.bottomMargin = 2.5F.dp.toInt()
            params.topMargin = 2.5F.dp.toInt()
            params.leftMargin = 3F.dp.toInt()
            params.rightMargin = 2F.dp.toInt()
            image.layoutParams = params
        }
    }
}